import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session
from backend.app.app.models.admin import admin_info_revise


# 获取所有信息
def get_old_ad(uid):
    try:
        tb_user_inf = session.query(md.TbAdministrator.User_Name,
                                    md.TbAdministrator.User_PWD,
                                    md.TbAdministrator.Belong_Depart,
                                    md.TbAdministrator.Job_Name,
                                    md.TbAdministrator.User_TruName,
                                    md.TbAdministrator.User_MobileNumber,
                                    md.TbAdministrator.User_Email,
                                    md.TbAdministrator.Register_Date,
                                    md.TbAdministrator.IsQualified,
                                    md.TbAdministrator.Remark).filter(md.TbAdministrator.User_ID == uid.user_id).first()

        return tb_user_inf
    except Exception as e:
        print('查找信息失败',e,e.__traceback__.tb_lineno)
        session.rollback()
        return False


def revise_admin(user: admin_info_revise):
    try:
        session.query(md.TbAdministrator).filter(md.TbAdministrator.User_ID == user.User_ID).update(
            {md.TbAdministrator.User_Name: user.User_Name,
             md.TbAdministrator.User_PWD: user.User_PWD,
             md.TbAdministrator.User_MobileNumber: user.User_MobileNumber,
             md.TbAdministrator.User_Email: user.User_Email})
        session.commit()
        print('更新管理员信息成功')
        return True
    except Exception as e:
        print('更新管理员信息失败',e,e.__traceback__.tb_lineno)
        session.rollback()
        return False


# 更新在线状态
def updata_online_tb(user_id, status):
    try:
        session.query(md.TbUser).filter(md.TbUser.User_ID == user_id).update(
            {md.TbUser.Online: status})
        session.commit()
        print('更新用户在线状态成功')
        return True
    except Exception as e:
        print('更新用户在线状态失败',e,e.__traceback__.tb_lineno)
        session.rollback()
        return False


# 管理员修改用户信息
# 根据user_id更新tb_user表
def admin_update_user(update_data: dict):
    try:
        session.query(md.TbUser).filter(md.TbUser.User_ID == update_data["user_id"]).update(
            {
                 md.TbUser.User_Name: update_data["user_name"],
                 md.TbUser.User_PWD: update_data["user_pwd"],
                 md.TbUser.Belong_Depart: update_data["belong_depart"],
                 md.TbUser.Job_Name: update_data["job_name"],
                 md.TbUser.User_TruName: update_data["user_truname"],
                 md.TbUser.User_MobileNumber: update_data["user_mobilenumber"],
                 md.TbUser.User_Email: update_data["user_email"],
                 md.TbUser.Register_Date: update_data["register_date"],
                 md.TbUser.IsQualified: update_data["isqualified"],
                 md.TbUser.Remark: update_data["remark"],
                 md.TbUser.Check_Date: update_data["Check_Date"],
                 md.TbUser.Check_Name: update_data["Check_Name"],
                 md.TbUser.Update_Date: update_data["Update_Date"],
                 md.TbUser.LegalPerson_ID: update_data["LegalPerson_ID"],
                 md.TbUser.User_Role: "普通用户"
             })
        session.commit()
        print('更新用户信息成功')
        return True
    except Exception as e:
        print('更新用户信息失败',e,e.__traceback__.tb_lineno)
        session.rollback()
        return False


# 管理员删除用户信息
def admin_del_user(user_id):
    try:
        session.query(md.TbUser).filter(md.TbUser.User_ID == user_id).delete()
        session.commit()
        print('删除用户成功')
        return True
    except Exception as e:
        print('删除用户失败',e,e.__traceback__.tb_lineno)
        session.rollback()
        return False


# 添加新用户注册信息
"""
用户名，密码，所在部门，岗位名称，真实姓名，手机号码，电子邮箱，注册日期，授权状态，备注
"""


def admin_add_user(update_date, check_date, user_id,
                   user_name, user_pwd, belong_depart, job_name,
                   user_truname, user_mobilenumber, user_email, register_date, isqualified, remark,
                   legalperson_id, user_role, check_name):
    new_user = md.TbUser(User_ID=user_id,
                         User_Name=user_name,
                         User_PWD=user_pwd,
                         User_Role=user_role,
                         Belong_Depart=belong_depart,
                         Job_Name=job_name,
                         User_TruName=user_truname,
                         User_MobileNumber=user_mobilenumber,
                         User_Email=user_email,
                         Register_Date=register_date,
                         IsQualified=isqualified,
                         Check_Date=check_date,
                         Check_Name=check_name,
                         Update_Date=update_date,
                         Remark=remark,
                         LegalPerson_ID=legalperson_id)
    try:
        session.add(new_user)
        session.commit()
        print('添加用户信息成功')
        session.close()
        return True
    except Exception as e:
        print('新建用户失败')
        session.rollback()
        return False


# 存储管理员界面用户信息表1格数据,并将相同管理员上一条数据删除 TbAdminPage1Info
def save_info_table_data1(user_id, proj_id, page_data):
    try:
        session.query(md.TbAdminPage1Info).filter(md.TbAdminPage1Info.User_ID == user_id and
                                                  md.TbAdminPage1Info.Proj_ID == proj_id).delete()

        table_data = md.TbAdminPage1Info(User_ID=user_id,
                                         Proj_ID=proj_id,
                                         Page_Data=page_data)
        session.add(table_data)
        session.commit()
        print('添加数据成功')
        session.close()
        return True
    except Exception as e:
        print('添加数据失败')
        return False


# 存储管理员界面用户信息表2数据,并将相同管理员上一条数据删除 TbAdminPage2Info
def save_info_table_data2(user_id, proj_id, page_data):
    try:
        session.query(md.TbAdminPage2Info).filter(md.TbAdminPage2Info.User_ID == user_id and
                                                  md.TbAdminPage2Info.Proj_ID == proj_id).delete()
        table_data = md.TbAdminPage2Info(User_ID=user_id,
                                         Proj_ID=proj_id,
                                         Page_Data=page_data)
        session.add(table_data)
        session.commit()
        print('添加数据成功')
        session.close()
        return True
    except Exception as e:
        print('添加数据失败')
        return False


# 从管理员界面用户信息表1中返回数据
def reurn_info_table1_data(user_id,proj_id):
    try:
        data = session.query(md.TbAdminPage1Info.Page_Data).filter(md.TbAdminPage1Info.User_ID == user_id and
                                                              md.TbAdminPage1Info.Proj_ID == proj_id).first()
        print(data)
        return data
    except Exception as e:
        return False


# 从管理员界面用户信息表2中返回数据
def reurn_info_table2_data(user_id,proj_id):
    try:
        data = session.query(md.TbAdminPage2Info.Page_Data).filter(md.TbAdminPage2Info.User_ID == user_id and
                                                              md.TbAdminPage2Info.Proj_ID == proj_id).first()
        print(data)
        return data
    except Exception as e:
        return False


# 储存管理员框架界面表格数据
def save_admin_frame_data(user_id, proj_id, frame_data):
    try:
        session.query(md.TbAdminFrameTable).filter(md.TbAdminFrameTable.User_ID == user_id and
                                                  md.TbAdminFrameTable.Proj_ID == proj_id).delete()
        table_data = md.TbAdminFrameTable(User_ID=user_id,
                                         Proj_ID=proj_id,
                                         frame_Data=frame_data)
        session.add(table_data)
        session.commit()
        print('添加数据成功')
        session.close()
        return True
    except Exception as e:
        print('添加数据失败')
        return False


# 返回管理员框架界面表格数据
def reurn_admin_frame_data(user_id,proj_id):
    try:
        data = session.query(md.TbAdminFrameTable.frame_Data).filter(md.TbAdminFrameTable.User_ID == user_id and
                                                              md.TbAdminFrameTable.Proj_ID == proj_id).first()
        print(data)
        return data
    except Exception as e:
        return False

if __name__ == '__main__':
    reurn_admin_frame_data(user_id=2,proj_id=2)